package logic

import (
	"context"
	"gorm.io/gorm"
	"weee/models"

	"weee/orderrpc/internal/svc"
	"weee/orderrpc/orderrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AliPayLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAliPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliPayLogic {
	return &AliPayLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AliPay todo: 订单支付（支付宝）
func (l *AliPayLogic) AliPay(in *orderrpc.AliPayRequest) (*orderrpc.AliPayResponse, error) {
	// todo: add your logic here and delete this line
	order, _ := models.GetOrderInfo(models.Order3{
		Model: gorm.Model{ID: uint(in.OrderId)},
	})
	if order.ID == 0 {
		return &orderrpc.AliPayResponse{
			Code: 400,
			Msg:  "订单不存在",
		}, nil
	}
	order.PayType = "支付宝"
	order.Status = "已支付"
	_, err := models.SaveOrder(order)
	if err != nil {
		return &orderrpc.AliPayResponse{
			Code: 400,
			Msg:  "更改状态失败",
		}, nil
	}
	return &orderrpc.AliPayResponse{
		Code:    200,
		Msg:     "查询成功",
		OrderSn: order.OrderSn,
		Money:   float32(order.Price),
	}, nil
}
